
package com.google.code.lebar;


import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.appengine.api.datastore.GeoPt;
import com.google.code.lebar.bean.builders.LayarRequestBuilder;
import com.google.code.lebar.bean.builders.LayarResponseBuilder;
import com.google.code.lebar.bean.request.LayarRequest;
import com.google.code.lebar.bean.response.LayarResponse;


public class MainServlet extends HttpServlet
{
    /**
     * 
     */
    private static final long serialVersionUID = 5745320446046842498L;

    public MainServlet()
    {
        // LayarResponse tResponse = LayarResponseBuilder.forLayer(
        // Layers.TORESTO ).
    }

    @Override
    public void doGet( HttpServletRequest req, HttpServletResponse resp ) throws IOException
    {
        LayarRequest tRequest = LayarRequestBuilder.fromParameterMap( req.getParameterMap() );

        // System.out.println( "Request received from layer = " +
        // tRequest.getLayerName() );

        LayarResponse tResponse = LayarResponseBuilder.fromRequest( tRequest ).build();

        writeLayarResponse( tResponse, resp );
    }

    private void writeLayarResponse( LayarResponse pLayarResponse, HttpServletResponse pHttpResponse )
    {
        try
        {
            pHttpResponse.setContentType( "application/json" );
            pHttpResponse.getWriter().write( new ObjectMapper().writeValueAsString( pLayarResponse ) );
            pHttpResponse.getWriter().close();
        }
        catch ( Exception ex )
        {
            ex.printStackTrace( System.out );
        }
    }
}
